package com.SeleniumTestsGoogleChrome;

import com.thoughtworks.selenium.SeleneseTestBase;
import com.thoughtworks.selenium.Selenium;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.util.regex.Pattern;

public class ModifyReservationTestCases extends SeleneseTestBase {

	@Before
	public void setUp() throws Exception {
		File file = new File(
				"C:/Users/Steven/Documents/4th Year/EECE 419/chromedriver.exe");
		System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
		WebDriver driver = new ChromeDriver();
		String baseUrl = "http://localhost:8080/Website/";
		selenium = new WebDriverBackedSelenium(driver, baseUrl);
	}
	
	// Created by Steven Chow
	// Use Case: Modify Reservation
	// Scenario 1: Successful Modification
	// Test Case 1: Modify current Flight
	@Test
	public void testModifyReservationSC1TC1() throws Exception {
		// Go to website
		selenium.open("");
		selenium.click("css=div.tile.bg-color-red > div.tile-content.icon");
		selenium.waitForPageToLoad("30000");
		// Login
		selenium.type("name=username", "User4");
		selenium.type("name=password", "pass4");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// We in Customer portal?
		assertEquals("Customer Portal", selenium.getTitle());
		// Search Flights
		selenium.click("css=div.tile-content.icon");
		selenium.waitForPageToLoad("30000");
		// No filter
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		selenium.click("xpath=(//input[@name='r1'])[2]");
		// 2nd one
		// Make Reservation for Flight 1001
		selenium.click("css=div > input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Fill in all the stuff
		selenium.type("name=firstName", "Hot");
		selenium.type("name=lastName", "Girl");
		selenium.type("name=seatID", "F25");
		selenium.click("//div[@id='page1']/div/label[2]/span");
		selenium.click("xpath=(//input[@name='mealOption'])[2]");
		// Make the reservation
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Check if we are in the payment page
		assertTrue(selenium.isTextPresent("Payment"));
		assertTrue(selenium.isTextPresent("Price: 100"));
		// Choose Points
		selenium.type("name=point", "100");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Check if we made a reservation
		assertEquals("1 new reservation added!",
				selenium.getText("//html/body/div/div[2]/div/div/div/div/h4"));
		selenium.click("css=div.tile.bg-color-blue > div.tile-content.icon > img");
		selenium.waitForPageToLoad("30000");
		// Check if we are on the Reservation List Page
		assertEquals("Reservation List", selenium.getTitle());
		// Check if our reservation is there
		assertTrue(selenium.isTextPresent("Hot Girl"));
		selenium.click("name=r1");
		selenium.click("name=view");
		selenium.waitForPageToLoad("30000");
		// Let's change our reservation
		selenium.click("css=div.page-control.span11 > div.frames > form > input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Check if you get a pay telling u that you will get a refund
		selenium.type("name=seat", "E10");
		selenium.select("name=meal", "label=Vegetarian");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Are we in the Payment Page?
		assertEquals("Make Payment", selenium.getTitle());
		// Did the reservation change?
		assertTrue(selenium.isTextPresent("Reservation modified"));
		// Go back and check if the reservation got changed
		selenium.click("//a[contains(@href, '/Website/ViewResListURL?username=User4')]");
		selenium.waitForPageToLoad("30000");
		selenium.click("name=r1");
		selenium.click("name=view");
		selenium.waitForPageToLoad("30000");
		assertEquals(
				"Hot Girl E11 Business vegetarian",
				selenium.getText("//html/body/div/div[2]/div/div[2]/div/div/table/tbody/tr"));

	}

	// Created by Steven Chow
	// Use Case: Modify Reservation
	// Scenario 1: Successful Modification
	// Test Case 2 Change Flight
	@Test
	public void testModifyReservationSC1TC2() throws Exception {
		// Go to website
		selenium.open("");
		selenium.click("css=div.tile.bg-color-red > div.tile-content.icon");
		selenium.waitForPageToLoad("30000");
		// Login
		selenium.type("name=username", "User4");
		selenium.type("name=password", "pass4");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// We in Customer portal?
		assertEquals("Customer Portal", selenium.getTitle());
		selenium.click("css=div.tile.bg-color-blue > div.tile-content.icon > img");
		selenium.waitForPageToLoad("30000");
		// Check if we are on the Reservation List Page
		assertEquals("Reservation List", selenium.getTitle());
		// Check if our reservation is there
		assertTrue(selenium.isTextPresent("Hot Girl"));
		selenium.click("name=r1");
		selenium.click("name=view");
		selenium.waitForPageToLoad("30000");
		// Check if shit is there
		// Change from Flight 1001 to Flight 1002
		assertEquals(
				"Hot Girl E10 Business vegetarian",
				selenium.getText("//html/body/div/div[2]/div/div[2]/div/div/table/tbody/tr"));
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		selenium.click("xpath=(//input[@name='r1'])[3]");
		selenium.click("css=div > input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		selenium.type("name=firstName", "Hot");
		selenium.type("name=lastName", "Girl");
		selenium.type("name=seatID", "C26");
		selenium.click("//div[@id='page1']/div/label[3]/span");
		selenium.click("xpath=(//input[@name='mealOption'])[3]");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		selenium.click("name=credit");
		selenium.type("name=credit", "100");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		selenium.click("css=div.tile.bg-color-blue > div.tile-content.icon");
		selenium.waitForPageToLoad("30000");
		selenium.click("name=r1");
		selenium.click("name=view");
		selenium.waitForPageToLoad("30000");
		// Make an assertion here
		assertEquals(
				"Hot Girl C26 Economy Vegetarian",
				selenium.getText("//html/body/div/div[2]/div/div[2]/div/div/table/tbody"));
		selenium.click("//a[contains(@href, '/Website/ViewResListURL?username=User4')]");
		selenium.waitForPageToLoad("30000");
		selenium.click("//a[contains(@href, '/Website/index_loggedin.jsp?username=User4')]");
		selenium.waitForPageToLoad("30000");
		selenium.click("css=div.tile-content.icon");
		selenium.waitForPageToLoad("30000");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Check if flight 1001 has #of seats remaining back to 122
		assertEquals(
				"1001 2012-10-10 14:00 Air Canada Los Angeles Vancouver 200.0 122",
				selenium.getText("//html/body/div/div[2]/div/div/div/div/div[2]/form/table/tbody/tr[2]"));
	}

	// Created by Steven Chow
	// Use Case: Modify Reservation
	// Scenario 2: UnSuccessful Modification
	// Test Case 3: Invalid Seat, 1st and last Name
	@Test
	public void testModifyReservationSC2TC1() throws Exception {
		// Go to website
		selenium.open("");
		selenium.click("css=div.tile.bg-color-red > div.tile-content.icon");
		selenium.waitForPageToLoad("30000");
		// Login
		selenium.type("name=username", "ReserveUser1");
		selenium.type("name=password", "pass");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// We in Customer portal?
		assertEquals("Customer Portal", selenium.getTitle());
		// Search Flights
		selenium.click("css=div.tile-content.icon");
		selenium.waitForPageToLoad("30000");
		// No filter
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		selenium.click("xpath=(//input[@name='r1'])[2]");
		// 2nd one
		// Make Reservation for Flight 1001
		selenium.click("css=div > input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Fill in all the stuff
		selenium.type("name=firstName", "Hot");
		selenium.type("name=lastName", "Girl");
		selenium.type("name=seatID", "B21");
		selenium.click("//div[@id='page1']/div/label[2]/span");
		selenium.click("xpath=(//input[@name='mealOption'])[2]");
		// Make the reservation
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Check if we are in the payment page
		assertTrue(selenium.isTextPresent("Payment"));
		assertTrue(selenium.isTextPresent("Price: 100"));
		// Choose Points
		selenium.type("name=credit", "100");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Check if we made a reservation
		assertEquals("1 new reservation added!",
				selenium.getText("//html/body/div/div[2]/div/div/div/div/h4"));
		selenium.click("css=div.tile.bg-color-blue > div.tile-content.icon > img");
		selenium.waitForPageToLoad("30000");
		// Check if we are on the Reservation List Page
		assertEquals("Reservation List", selenium.getTitle());
		// Check if our reservation is there
		assertTrue(selenium.isTextPresent("Hot Girl"));
		selenium.click("name=r1");
		selenium.click("name=view");
		selenium.waitForPageToLoad("30000");
		// Let's change our reservation
		selenium.click("css=div.page-control.span11 > div.frames > form > input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Fill in our stuff like change seat and Meal
		selenium.type("name=seat", "Z20");
		selenium.select("name=meal", "label=Vegetarian");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		assertFalse(selenium.isTextPresent("Reservation modified"));
	}

	// Created by Steven Chow
	// Use Case: Modify Reservation
	// Scenario 2: UnSuccessful Modification
	// Test Case 2: Picked a Seat that is already taken
	@Test
	public void testModifyReservationSC2TC2() throws Exception {
		selenium.open("");
		selenium.click("css=div.tile.bg-color-red > div.tile-content.icon");
		selenium.waitForPageToLoad("30000");
		// Login
		selenium.type("name=username", "ReserveUser2");
		selenium.type("name=password", "pass");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// We in Customer portal?
		assertEquals("Customer Portal", selenium.getTitle());
		// Search Flights
		selenium.click("css=div.tile-content.icon");
		selenium.waitForPageToLoad("30000");
		// No filter
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		selenium.click("xpath=(//input[@name='r1'])[2]");
		// 2nd one
		// Make Reservation for Flight 1001
		selenium.click("css=div > input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Fill in all the stuff
		selenium.type("name=firstName", "Cute");
		selenium.type("name=lastName", "Girl");
		selenium.type("name=seatID", "B21");
		selenium.click("//div[@id='page1']/div/label[2]/span");
		selenium.click("xpath=(//input[@name='mealOption'])[2]");
		// Make the reservation
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Check if we are in the payment page
		assertTrue(selenium.isTextPresent("Payment"));
		assertTrue(selenium.isTextPresent("Price: 100"));
		// Choose Points
		selenium.type("name=credit", "100");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Check if we made a reservation
		assertEquals("1 new reservation added!",
				selenium.getText("//html/body/div/div[2]/div/div/div/div/h4"));
		selenium.click("css=div.tile.bg-color-blue > div.tile-content.icon > img");
		selenium.waitForPageToLoad("30000");
		// Check if we are on the Reservation List Page
		assertEquals("Reservation List", selenium.getTitle());
		// Check if our reservation is there
		assertTrue(selenium.isTextPresent("Cute Girl"));
		selenium.click("name=r1");
		selenium.click("name=view");
		selenium.waitForPageToLoad("30000");
		// Let's change our reservation
		selenium.click("css=div.page-control.span11 > div.frames > form > input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Fill in our stuff like change seat and Meal
		selenium.type("name=seat", "E10");
		selenium.select("name=meal", "label=Vegetarian");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		assertTrue(selenium
				.isTextPresent("SeatID E11 in 1001 is reserved by another customer."));
	}

	// Created by Steven Chow
	// Use Case: Modify Reservation
	// Scenario 2: UnSuccessful Modification
	// Test Case 3: Picked a Flight that is Full
	@Test
	public void testModifyReservationSC2TC3() throws Exception {
		selenium.open("");
		selenium.click("css=div.tile.bg-color-red > div.tile-content.icon");
		selenium.waitForPageToLoad("30000");
		// Login
		selenium.type("name=username", "ReserveUser2");
		selenium.type("name=password", "pass");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// We in Customer portal?
		assertEquals("Customer Portal", selenium.getTitle());
		selenium.click("css=div.tile.bg-color-blue > div.tile-content.icon");
		selenium.waitForPageToLoad("30000");
		selenium.click("name=r1");
		selenium.click("name=view");
		selenium.waitForPageToLoad("30000");
		// Let's change our flight here
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Select a flight that is full
		selenium.click("css=div > input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Are we getting a message saying that the flight is full?
		assertTrue(selenium.isTextPresent("This Flight is fully booked."));
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
	}

	// Created by Steven Chow
	// Use Case: Modify Reservation
	// Scenario 2: Cancelled Modification
	// Test Case 4: Not enough Points
	@Test
	public void testModifyReservationSC2TC4() throws Exception {
		selenium.open("");
		selenium.click("css=div.tile.bg-color-red > div.tile-content.icon");
		selenium.waitForPageToLoad("30000");
		// Login
		selenium.type("name=username", "ReserveUser3");
		selenium.type("name=password", "pass");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// We in Customer portal?
		assertEquals("Customer Portal", selenium.getTitle());
		// Search Flights
		selenium.click("css=div.tile-content.icon");
		selenium.waitForPageToLoad("30000");
		// No filter
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		selenium.click("xpath=(//input[@name='r1'])[2]");
		// 2nd one
		// Make Reservation for Flight 1001
		selenium.click("css=div > input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Fill in all the stuff
		selenium.type("name=firstName", "Sexy");
		selenium.type("name=lastName", "Girl");
		selenium.type("name=seatID", "B22");
		selenium.click("//div[@id='page1']/div/label[2]/span");
		selenium.click("xpath=(//input[@name='mealOption'])[2]");
		// Make the reservation
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Check if we are in the payment page
		assertTrue(selenium.isTextPresent("Payment"));
		assertTrue(selenium.isTextPresent("Price: 100"));
		// Choose Points
		selenium.type("name=credit", "100");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Check if we made a reservation
		assertEquals("1 new reservation added!",
				selenium.getText("//html/body/div/div[2]/div/div/div/div/h4"));
		selenium.click("css=div.tile.bg-color-blue > div.tile-content.icon > img");
		selenium.waitForPageToLoad("30000");
		// Check if we are on the Reservation List Page
		assertEquals("Reservation List", selenium.getTitle());
		// Check if our reservation is there
		assertTrue(selenium.isTextPresent("Sexy Girl"));
		selenium.click("name=r1");
		selenium.click("name=view");
		selenium.waitForPageToLoad("30000");
		// Let's change our reservation
		selenium.click("css=div.page-control.span11 > div.frames > form > input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Fill in our stuff like change seat and Meal
		selenium.type("name=seat", "A10");
		selenium.select("name=meal", "label=Vegetarian");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		assertTrue(selenium.isTextPresent("Payment"));
	}

	// Created by Steven Chow
	// Use Case: Modify Reservation
	// Scenario 3: Cancel Modification
	// Test Case 1: Cancel it! Duh
	@Test
	public void testModifyReservationSC3TC1() throws Exception {
		selenium.open("");
		selenium.click("css=div.tile.bg-color-red > div.tile-content.icon");
		selenium.waitForPageToLoad("30000");
		// Login
		selenium.type("name=username", "ReserveUser2");
		selenium.type("name=password", "pass");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// We in Customer portal?
		assertEquals("Customer Portal", selenium.getTitle());
		selenium.click("css=div.tile.bg-color-blue > div.tile-content.icon");
		selenium.waitForPageToLoad("30000");
		selenium.click("name=r1");
		selenium.click("name=view");
		selenium.waitForPageToLoad("30000");
		// Let's change our flight here
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Select a flight that is full
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		selenium.click("name=r1");
		selenium.click("css=div > input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// AHH forget it, I'm not going to change my flight
		selenium.click("//a[contains(@href, '../Website/index_loggedin.jsp?username=ReserveUser2')]");
		selenium.waitForPageToLoad("30000");
		selenium.click("css=div.tile.bg-color-blue > div.tile-content.icon > img");
		selenium.waitForPageToLoad("30000");
		// Check if my reservation is still there
		assertTrue(selenium.isTextPresent("Cute Girl"));
	}
	@After
	public void tearDown() throws Exception {
		selenium.stop();
	}

}
